package com.yxq.mdt.app.modules.splash.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.Constant;
import com.yxq.mdt.app.dialog.CostumerDialog;
import com.yxq.mdt.app.modules.login.ui.LoginActivity;
import com.yxq.mdt.app.modules.main.ui.MainActivity;
import com.yxq.mdt.app.modules.splash.adapter.GuideViewPageAdapter;
import com.yxq.mdt.app.net.API;
import com.yxq.mdt.app.widgets.pagemenulibrary.IndicatorView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import wendu.dsbridge.DWebView;

/**
 * 引导页面 App第一次安装会展示
 */
public class GuideActivity extends BaseActivity {

    @BindView(R.id.view_pager)
    ViewPager viewPager;
    GuideViewPageAdapter guideViewPageAdapter;
    @BindView(R.id.indicator_view)
    IndicatorView indicatorView;

    @Override
    public int getLayoutId() {
        setFullScreen(true);
        return R.layout.activity_guide;
    }

    @Override
    public void initView() {
        dialog();
        List<View> viewList = new ArrayList<>();
        viewList.add(LayoutInflater.from(this).inflate(R.layout.layout_guide_1, null));
        viewList.add(LayoutInflater.from(this).inflate(R.layout.layout_guide_2, null));
        viewList.add(LayoutInflater.from(this).inflate(R.layout.layout_guide_3, null));
        viewList.add(LayoutInflater.from(this).inflate(R.layout.layout_guide_4, null));
        guideViewPageAdapter = new GuideViewPageAdapter(viewList);
        guideViewPageAdapter.setGuideViewPageAdapterInterface(() -> {
            Intent intent;
            if (getIntent().getIntExtra("type", 1) == 1) {
                intent = new Intent(GuideActivity.this, LoginActivity.class);
            } else {
                intent = new Intent(GuideActivity.this, MainActivity.class);
            }
            startActivity(intent);
            finish();
        });
        viewPager.setAdapter(guideViewPageAdapter);
        indicatorView.setIndicatorCount(viewList.size());
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indicatorView.setCurrentIndicator(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    private void dialog() {
        CostumerDialog dialog = new CostumerDialog.Builder(this, R.style.custom_dialog_no_match)
                .setContentView(R.layout.dialog_first_user_layout)
                .setCancelable(false)
                .setOutTouchCancelable(false)
                .create();
        DWebView dWebView = dialog.findViewById(R.id.webView);
        dWebView.loadUrl(API.PRIVACY_POLICY);
        CheckBox checkBox = dialog.findViewById(R.id.user_check_box);
        Button button = dialog.findViewById(R.id.user_btn_next);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                button.setEnabled(isChecked);
            }
        });
        button.setOnClickListener(v -> {
            if (checkBox.isChecked()) {
                Constant.setFirstInstall();
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}
